/*!
//      
//      \file	k2k_AudioPlayer.c
//      
//      \author	Copyright 2010 Valentina Vuksic <vuksv1@bfh.ch>, Beat Liechti <liecb2@bfh.ch>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
//
//      Abstract Data Type for state machine
//      
//      \brief		CAS-EBX-03 Projektarbeit 
//      			Valentina Vuksic (VUKSV1), Beat Liechti (LIECB2)
//      			k2k_AudioPlayer ADT for the k2k (Kaffee2000) application
//      \version	1.0
//      \date		2010-05-01
//      \remark		Last Modifications:
//      \remark  	v1.0, VUKSV1/LIECB2, 2010-05-01 Initial release
//
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>

#include "k2k_AudioPlayer.h"
#include "k2k_StateMachineTypes.h"

/*! sound clips are played with mplayer */
#define MPLAYER_JINGLE	"mplayer /usr/local/share/k2k_audio/audio_clip_loaded.wav"
#define MPLAYER_RDY1	"mplayer /usr/local/share/k2k_audio/audio_clip_rdy1.wav";
#define MPLAYER_RDY2	"mplayer /usr/local/share/k2k_audio/audio_clip_rdy2.wav";
#define MPLAYER_RSTR	"mplayer /usr/local/share/k2k_audio/audio_clip_rstr.wav";
#define MPLAYER_ESPR	"mplayer /usr/local/share/k2k_audio/audio_clip_espr.wav";
#define MPLAYER_MLKC	"mplayer /usr/local/share/k2k_audio/audio_clip_mlkc.wav";
#define MPLAYER_CLN		"mplayer /usr/local/share/k2k_audio/audio_clip_clean.wav";
#define MPLAYER_ERROR	"mplayer /usr/local/share/k2k_audio/audio_clip_error.wav";
#define MPLAYER_EXIT	"mplayer /usr/local/share/k2k_audio/audio_clip_exit.wav";

/* -- static function declarations -----------------------------------*/

char *GetPlayCommand(k2k_AudioClip clip);

/* -- regular functions ----------------------------------------------*/

/*!
//
// Fork an audio player, play a clip, signal when done
// 
// \return nothing
//
*/
void k2k_AudioPlaySignal(k2k_AudioClip clip, k2k_Signal *signal)
{
	/*! get the command to play the requested clip */
	char *playClip = GetPlayCommand(clip);
	/*! forking to do non-blocking call to mplayer, signaling SIGUSR1 when done */
	pid_t pid = fork();
	if (pid == 0)
	{
		/*! child ==> launch mplayer */
		int retMplayer = system(playClip);
		if (retMplayer != 0)
		{
			printf("ERROR: Call to '%s' returned %x\n", playClip, retMplayer);
		}
		/*! signal that we are done */
		k2k_Signal_Raise(signal);
		/*! exit child */
		exit(retMplayer);
	}		
}

/*!
//
// Fork an audio player and play a clip without signaling
// 
// \return nothing
//
*/
void k2k_AudioPlayNoSignal(pid_t *pid, k2k_AudioClip clip)
{
	/*! get the command to play the requested clip */
	char *playClip = GetPlayCommand(clip);
	/*! add an ampersand  to the command to detach the child process 
	 * strlen(playclip) + 3 ==> space, ampersand, \0 */
	char *playClipAmp = malloc(strlen(playClip) + 3);
	sprintf(playClipAmp, "%s &", playClip);
	int retMplayer = system(playClipAmp);
	if (retMplayer != 0)
	{
		printf("ERROR: Call to '%s' returned %x\n", playClip, retMplayer);
	}
	return;
}

/*!
//
// Play the clip with a blocking call to the audio player
// 
// \return the audio play command 
//
*/
void k2k_AudioPlayBlocked(k2k_AudioClip clip)
{
	/*! get the command to play the requested clip */
	char *playClip = GetPlayCommand(clip);
	/*! do blocking call to mplayer */
	int retMplayer = system(playClip);
	if (retMplayer != 0)
	{
		printf("ERROR: Call to '%s' returned %x\n", playClip, retMplayer);
	}
}

/*!
//
// Get the audio play command for an audio clip
// 
// \return the audio play command 
//
*/
char *GetPlayCommand(k2k_AudioClip clip)
{
	char *playClip;
	switch(clip)
	{
		case CLIP_JINGLE:
			playClip = MPLAYER_JINGLE;
			break;
		case CLIP_RDY1:
			playClip = MPLAYER_RDY1;
			break;	
		case CLIP_RDY2:
			playClip = MPLAYER_RDY2;
			break;	
		case CLIP_RSTR:
			playClip = MPLAYER_RSTR;
			break;		
		case CLIP_ESPR:
			playClip = MPLAYER_ESPR;
			break;		
		case CLIP_MLKC:
			playClip = MPLAYER_MLKC;
			break;	
		case CLIP_CLN:
			playClip = MPLAYER_CLN;
			break;				
		case CLIP_EXIT:
			playClip = MPLAYER_EXIT;
			break;				
		default:
			playClip = MPLAYER_ERROR;		
			break;
	}
	return playClip;
}
